angular.module('login', [])

.controller 'LoginCtrl', ($location, userData, $scope, $state, $http, $ionicHistory
, Toast, $localStorage) ->
  $scope.account=[]
  $scope.account.phone=$localStorage.phone
  $scope.account.password=$localStorage.password
  if $localStorage.ifKeep==true
    $scope.keepPw="ion-ios-checkmark-outline unsure"
  else
    $scope.keepPw="ion-ios-circle-outline unsure";
    $localStorage.ifKeep=false
  #记住密码
  $scope.toKeepWd = ->
    if $localStorage.ifKeep==false
      $scope.keepPw="ion-ios-checkmark-outline unsure";
      $localStorage.ifKeep=true;
    else
      $scope.keepPw="ion-ios-circle-outline unsure";
      $localStorage.ifKeep=false;
  #登陆
  $scope.login = ->
    $http.get('/crewLogin'+'?phone='+$scope.account.phone+'&password='+$scope.account.password)
      .success (resp) ->
        if resp.code==0
          #登陆成功
          $localStorage.token=resp.message.token
          if $localStorage.ifKeep==true
            $localStorage.phone=$scope.account.phone
            $localStorage.password=$scope.account.password
          else
            $localStorage.phone=''
            $localStorage.password=''
          $state.go('tab.home')
          console.log(resp)
        else
          #登陆失败
          Toast.showMsg(resp.message)
      .error (resp, status) ->
        Toast.showMsg "获取账号数据失败"
        console.log(resp)

